package com.firewolf.oa.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.firewolf.oa.base.BaseAction;
import com.firewolf.oa.constant.OAConstant;
import com.firewolf.oa.entities.Department;
import com.firewolf.oa.entities.Role;
import com.firewolf.oa.entities.User;
import com.firewolf.oa.utils.DepartmentUtil;
import com.opensymphony.xwork2.ActionContext;

/**
 * 用户管理控制器
 * 
 * @author liuxing
 *
 */
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
	

	private static final long serialVersionUID = 9210877593801795883L;

	private Long deptId;

	private Long[] roleIds;
	
	
	/**
	 * 去登录页面
	 * @return
	 */
	public String toLogin(){
		return "login";
	}
	
	
	/**
	 * 登录
	 * @return
	 */
	public String login(){
		
		//根据账号密码查询用户
		User user = userService.findUserByNameAndPwd(model.getLoginName(),model.getPassword());
		if(user != null){
			//登录成功，把用户信息保存到session，跳转到主页
			ActionContext.getContext().getSession().put(OAConstant.CURRENT_USER, user);
			return "index";
		}else{
			//登录失败，添加错误信息，跳转到登录页面
			addFieldError("login", "用户名或者密码错误");
			return "login";
		}
		
	}
	
	/**
	 * 退出登录
	 * @return
	 */
	public String logout(){
		//注销登录
		ActionContext.getContext().getSession().remove(OAConstant.CURRENT_USER);
		//调到注销页面
		return "logout";
	}

	/**
	 * 去添加页面
	 * 
	 * @return
	 */
	public String toAdd() {
		// 获取所有的部门信息，用于下拉框
		List<Department> departments = deptService.findTopDepartments();
		List<Department> deptList = DepartmentUtil.getAllDepartmentTree("┣", departments);
		ActionContext.getContext().put("deptList", deptList);
		// 获取岗位信息
		try {
			List<Role> roleList = roleService.findAll();
			ActionContext.getContext().put("roleList", roleList);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "save";
	}

	/**
	 * 去修改页面
	 * 
	 * @return
	 */
	public String toUpdate() {
		try {
			// 获取用户信息，把用户信息放入值栈，用于回显
			User user = userService.findById(model.getId());
			ActionContext.getContext().getValueStack().push(user);
			
			// 获取所有的部门信息，用于显示部门立碑哦啊
			List<Department> departments = deptService.findTopDepartments();
			List<Department> deptList = DepartmentUtil.getAllDepartmentTree("┣", departments);
			ActionContext.getContext().put("deptList", deptList);
			// 获取岗位信息，用于显示岗位列表
			List<Role> roleList = roleService.findAll();
			ActionContext.getContext().put("roleList", roleList);
			
			//设置岗位id，用于回显
			List<Long> roleIdList = new ArrayList<>();
			Set<Role> roles = user.getRoles();
			for (Role r : roles) {
				roleIdList.add(r.getId());
			}
			roleIds = roleIdList.toArray(new Long[0]);
			
			//设置部门ID，用于回显
			Department department = user.getDepartment();
			if (department != null)
				deptId = user.getDepartment().getId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "save";
	}

	/**
	 * 添加用户
	 * 
	 * @return
	 */
	public String add() {
		try {
			Department dept = deptId == null ? null : deptService.findById(deptId);
			model.setDepartment(dept);
			List<Role> roleList = roleIds == null ? new ArrayList<Role>() : roleService.getByIds(roleIds);
			model.setRoles(new HashSet<>(roleList));
			model.setPassword("1234");
			userService.save(model);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	/**
	 * 修改用户
	 * 
	 * @return
	 */
	public String update() {
		try {
			Department dept = deptId == null ? null : deptService.findById(deptId);
			model.setDepartment(dept);
			List<Role> roleList = roleIds == null ? new ArrayList<Role>() : roleService.getByIds(roleIds);
			model.setRoles(new HashSet<>(roleList));
			userService.update(model);
		} catch (Exception e) {
		}
		return "toList";
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	public String list() {
		try {
			List<User> userList = userService.findAll();
			ActionContext.getContext().put("userList", userList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 重置密码
	 * 
	 * @return
	 */
	public String resetPassword() {
		userService.resetPassword(model.getId());
		return "toList";
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	public String delete() {
		try {
			userService.delete(model.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public Long getDeptId() {
		return deptId;
	}

	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public Long[] getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(Long[] roleIds) {
		this.roleIds = roleIds;
	}
}
